<?php
/**
 * TODO: clean&tidy (phpDoc stuff!)
 *
 */
class TinyMVCView extends TinyMVCResource {

	private $pathResolver;
	private $namespace;

	/**
	 * @param TinyMVCPathResolver $pathResolver path resolver
	 * @param string $viewNamespace view namespace to separate different views
		*/
	public function __construct(TinyMVCPathResolver $pathResolver, $namespace =  '') {
		parent::__construct('view');

		//$this->elements = new TinyMVCSimpleContainer;
		$this->pathResolver = $pathResolver;
		$this->namespace = $namespace;
	}

	/**
	 * add element to view (because of overloading we're can use: __set(), set() and add() as well)
	 * @param string $key element key
	 * @param mixed $value element value
	 * @param bool $resolvePath true if we want resolve path
	 */
	public function addElement($key, $value, $resolvePath = true) {
		parent::addElement(($this->namespace ? $this->namespace . '.' . $key : $key), ($resolvePath ? $this->pathResolver->resolve($value) : $value));
	}

	/**
	 * get view file path (overloaded parent method for exception support)
	 * @param string view name
	 * @return string view path
	 */
	public function get($name) {
		$path = parent::get($name);
		if($path == null) {
			throw new TinyMVCException("Unknown view: $name");
		}
		else {
			if(!file_exists($path)) {
				throw new TinyMVCException("Unable to load view: $path file not found");
			}
			else {
				return $path;
			}
		}
	}

	public function __get($name) {
		return $this->get($name);
	}

	public function getElement($name) {
		return $this->get($name);
	}

}
